// pages/Pinyin/Pinyin.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    butao:"",
    list1: "",
    yanseindex:"",
    index: 1,
    dete: 80,
    wenid: "", //酒的id
    talk: "", //要输入的文字
    isfouce:true,
    imgurl: '',
    name: "",
    wenxiang: "",
    rukou: "",
    zhongduan: "",
    a1: 0,
    a2: 0,
    a3: 0,
    a4: 0,
    a5: 0,
    names:'',
    dis: true,
    yuwei: "",
    yanse: [{
      bag: "background:#431B09;",
      id:0,
      name: "糖蜜色"
    }, {
        bag: "background:#722D1E;",
        id:1,
      name: "雪利棕色"
    }, {
        bag: "background:#9E241C;",
        id:2,
      name: "古橡色"
    }, {
        bag: "background:#AD302C;",
        id:3,
      name: "深赭色"
    }, {
        bag: "background:#CE4E33;",
        id:4,
      name: "红木色"
    }, {
        bag: "background:#E06529;",
        id:5,
      name: "赤褐色"
    }, {
        bag: "background:#DC7415;",
        id:6,
      name: "茶色"
    }, {
        bag: "background:#E69505;",
        id:7,
      name: "黄褐色"
    }, {
        bag: "background:#E89E14;",
        id:8,
      name: "欧罗索学利色"
    }, {
        bag: "background:#EAAD08;",
        id:9,
      name: "焦黄色"
    }, {
        bag: "background:#F6BD2E;",
        id:10,
      name: "深黄铜色"
    }, {
        bag: "background:#F6C23C;",
        id:11,
      name: "阿蒙蒂亚雪利色"
    }, {
        bag: "background:#F5C94D;",
        id:12,
      name: "深金色"
    }, {
        bag: "background:#F7CF4A;",
        id:13,
      name: "琥珀色"
    }, {
        bag: "background:#F8D84A;",
        id:14,
      name: "古金色"
    }, {
        bag: "background:#F8DC56;",
        id:15,
      name: "金黄色"
    }, {
        bag: "background:#FBE166;",
        id:16,
      name: "玉米色"
    }, {
        bag: "background:#FCEA75;",
        id:17,
      name: "淡金色"
    }, {
        bag: "background:#FFEE97;",
        id:18,
      name: "淡稻草黄"
    }, {
        bag: "background:#FFFEDD;",
        id:19,
      name: "白葡萄酒"
    }, {
        bag: "background:#FFFFFF;",
        id:20,
      name: "清澈的金酒"
    }],
    titles: [], //标题
    title: [], //闻香
    titler: [], //入口
    titlez: [], //中段
    titley: [], //余味
    color: [],
  },
  click: function(e) {
 
      this.data.list1[e.currentTarget.dataset.ind].list[e.currentTarget.dataset.inds].active = !this.data.list1[e.currentTarget.dataset.ind].list[e.currentTarget.dataset.inds].active
      this.setData({
        list1: this.data.list1
      })
    
    // list1
    var that = this
    if (that.data.name == "闻香") {
      if (that.data.title.filter(item => item.name == e.currentTarget.dataset.id.name).length) {
        that.data.title.map((item, index) => {
          if (item.name == e.currentTarget.dataset.id.name) {
            that.data.title.splice(index, 1)
          }
        })
      } else {
        that.data.title.push(e.currentTarget.dataset.id)
      }
      that.setData({
        title: that.data.title,
        titles: that.data.title,
        indexs: e.currentTarget.dataset.index
      })
    } else if (that.data.name == "入口") {
      if (that.data.titler.filter(item => item.name == e.currentTarget.dataset.id.name).length) {
        that.data.titler.map((item, index) => {
          if (item.name == e.currentTarget.dataset.id.name) {
            that.data.titler.splice(index, 1)
          }
        })
      } else {
        that.data.titler.push(e.currentTarget.dataset.id)
      }
      that.setData({
        titler: that.data.titler,
        titles: that.data.titler,
        indexs: e.currentTarget.dataset.index
      })
    } else if (that.data.name == "中段") {
      if (that.data.titlez.filter(item => item.name == e.currentTarget.dataset.id.name).length) {
        that.data.titlez.map((item, index) => {
          if (item.name == e.currentTarget.dataset.id.name) {
            that.data.titlez.splice(index, 1)
          }
        })
      } else {
        that.data.titlez.push(e.currentTarget.dataset.id)
      }
      that.setData({
        titlez: that.data.titlez,
        titles: that.data.titlez,
        indexs: e.currentTarget.dataset.index
      })
    } else {
      if (that.data.titley.filter(item => item.name == e.currentTarget.dataset.id.name).length) {
        that.data.titley.map((item, index) => {
          if (item.name == e.currentTarget.dataset.id.name) {
            that.data.titley.splice(index, 1)
          }
        })
      } else {
        that.data.titley.push(e.currentTarget.dataset.id)
      }
      that.setData({
        titley: that.data.titley,
        titles: that.data.titley,
        indexs: e.currentTarget.dataset.index
      })
    }

  },
  isfouce(){
    this.setData({
      isfouce:false
    })
  },
  //左侧的tap切换
  leftclick: function(e) {
    let that = this
    that.setData({
      index: e.currentTarget.dataset.id
    })
    that.listdata(that.data.index)
  },
  //文本内容
  talk: function(e) {
    this.setData({
      isfouce:true,
      talk: e.detail.value
    })
  },
  //点击颜色
  yanse: function(e) {
    console.log(e,99999)
    if (this.data.yanse[e.currentTarget.dataset.index].active) {
    this.data.yanse[e.currentTarget.dataset.index].active = false;
    } else {
      this.data.yanse[e.currentTarget.dataset.index].active = true;
    }
    // console.log
    this.setData({
      color: e.currentTarget.dataset.item + ',' + e.currentTarget.dataset.name,
      yanseindex: e.currentTarget.dataset.index,
      yanse:this.data.yanse
    })
    console.log(this.data.yanse)
    
  },
  tijiaos: function() {
    console.log(this.data.color)
    var fen = (wx.getStorageSync("a1") * 1 + wx.getStorageSync("a2") * 1 + wx.getStorageSync("a3") * 1 + wx.getStorageSync("a4") * 1 + wx.getStorageSync("a5") * 1) / 5
    fen = fen.toFixed(1);
    let smell = '' //闻香
    let entry = '' //入口
    let middle = '' //中段
    let taste = '' //余味

    
    //循环闻香
    if (this.data.wenxiang == "") {
      smell=""
    } else {
      this.data.wenxiang.map(item => {
        if (smell == '') {
          smell += item.name
        } else {
          smell += `,${item.name}`
        }
      })
    }
    if (this.data.rukou == '') {
      entry=''
    } else {
      //循环入口
      this.data.rukou.map(item => {
        if (entry == '') {
          entry += item.name
        } else {
          entry += `,${item.name}`
        }
      })
    }
    if (this.data.zhongduan == '') {
      middle=""
    } else {
      //循环中段
      this.data.zhongduan.map(item => {
        if (middle == '') {
          middle += item.name
        } else {
          middle += `,${item.name}`
        }
      })
    }
    if (this.data.yuwei == '') {
      taste=""
    } else {
      //循环余味
      this.data.yuwei.map(item => {
        if (taste == '') {
          taste += item.name
        } else {
          taste += `,${item.name}`
        }
      })
    }if(this.data.color==""){
      this.setData({
        color:''
      })
    }
    let datas = {

      'wineid': wx.getStorageSync("wiedi"),
      'text': this.data.talk,
      'score': fen,
      'label_smell': smell,
      'label_entry': entry,
      'label_middle': middle,
      'label_taste': taste,
      'color':this.data.color,
    }
    app.request({
      url: '/insertNote',
      data: datas,
      method:'post',
      success: function(res) {
        if (res.code == 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'succes',
            duration: 500,
            mask: true
          })
          wx.navigateTo({
            url: '../works/index?id='+wx.getStorageSync("wiedi"),
          })
        }
        if(res.code==2011){
          wx.showToast({
            title: '请添加纯文字',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
        console.log(res)
      }
    });
  },
  //点击添加闻香
  butsrew: function() {
    this.setData({
      name: '闻香',
      dis: false,
      titles: ''
    })
    // wx.navigateTo({
    //   url: '../list/list?name=闻香',
    // })
  },
  //点击添加入口
  butr: function() {
    this.setData({
      name: '入口',
      titles: '',
      dis: false
    })
    // wx.navigateTo({
    //   url: '../list/list?name=入口',
    // })
  },
  //点击中段入口
  butz: function() {
    this.setData({
      name: '中段',
      titles: '',
      dis: false
    })
    // wx.navigateTo({
    //   url: '../list/list?name=中段',
    // })
  },
  //点击余味入口
  butys: function() {
    this.setData({
      name: '余味',
      titley:[],
      titles: '',
      dis: false
    })
    console.log(this.data.titles)
    // wx.navigateTo({
    //   url: '../list/list?name=余味',
    // })
  },
  buts: function() {
    let a1 = wx.getStorageSync('a1')
    let a2 = wx.getStorageSync('a2')
    let a3 = wx.getStorageSync('a3')
    let a4 = wx.getStorageSync('a4')
    let a5 = wx.getStorageSync('a5')
    console.log(this.data.title)


    let that = this;
    console.log(this.data.name)
    if (that.data.name == "闻香") {
      wenxiang: this.data.title
      wx.setStorageSync('title', this.data.title)
      that.setData({
        dis: true,
        wenxiang: this.data.title
      })
      // wx.navigateTo({
      //   url: '../Pinyin/Pinyin?title=' + this.data.title+'&a1='+a1+'&a2='+a2+'&a3='+a3+'&a4='+a4+'&a5='+a5,
      // })
    }
    else if (that.data.name == "入口") {


      that.setData({
        dis: true,
        rukou: this.data.titles
      })

      // wx.navigateTo({
      //   url: '../Pinyin/Pinyin',
      // })
    } else if (that.data.name == "中段") {
      that.setData({
        dis: true,
        zhongduan: this.data.titles
      })
    } else {
   
      that.setData({
        dis: true,
        yuwei: this.data.titles
      })

    }
    this.data.list1.map((item, i) => {
      item.list.map((item, ind) => {
        if (this.data.list1[i].list[ind].active) {
          this.data.list1[i].list[ind].active = false
        }
      })
    })
   
    this.setData({
      titler: [],
      titlez: [],
      title: [],
      titles: [],
      list1: this.data.list1,
      list2: this.data.list2,
      list3: this.data.list3,
      list4: this.data.list4,
      list5: this.data.list5,
      list6: this.data.list6,
      list7: this.data.list7,
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.img)
    let that = this;
    that.setData({
      wenid: options.id,
      imgurl: options.img,
      wenxiang: "",
      rukou: "",
      zhongduan: '',
      yuwei: '',
      a1: options.a1,
      a2: options.a2,
      a3: options.a3,
      a4: options.a4,
      a5: options.a5,
    })
   that.listdata(1)
  },
  listdata:function(e){
    let classname = e == 1 ? '葡萄酒香' : e == 2 ? '谷物' : e == 3 ? '果实' : e == 4 ? '馥郁' : e == 5 ? '泥煤' : e == 6 ? '酒尾' : e == 7 ? '硫磺' :'木质调'
    let that=this
    app.request({
      url: '/res',
      data:{'name':classname},
      method:'post',
      success: function (res) {
        console.log(res)
        that.setData({
          list1: res.data
        })
        console.log(that.data.list1[0].list)
        // that.setData({ list: that.data.list.concat(res.data.list), offset: that.data.offset + 1 })
      }
    })
  },
  add: function() {
    // this.setData({
    //   dete:this.data++
    // })
    console.log(this.data.dete)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})